package com.nengchuan.eic.nceconomy.component;


import com.nengchuan.frame.auth.util.AuthUtils;
import com.nengchuan.util.IdGenUtils;
import com.nsc.boot.sdk.common.utils.AuthUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * @description: Mybatis Plus实现控制字段自动填充处理类
 * @author: panwen
 * @create: 2020/05/21 15:10
 * @version：1.0.0
 */
@Component
@Primary
@Slf4j
public class MetaObjectHandler implements com.baomidou.mybatisplus.core.handlers.MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
//        if (metaObject.hasSetter("id") && StringUtils.isEmpty(metaObject.getValue("id"))) {
//            metaObject.setValue("id", IdGenUtils.genId());
//        }

        try {
            this.setInsertFieldValByName("createId", AuthUtil.getUserId(), metaObject);
            this.setInsertFieldValByName("createName", AuthUtil.getRealName(), metaObject);
        } catch (Exception var3) {
            log.warn("自动填充获取不到用户信息");
        }

        long now=System.currentTimeMillis();
        this.setInsertFieldValByName("createTime", now , metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        try {
            this.setUpdateFieldValByName("modifyId", AuthUtil.getUserId(), metaObject);
            this.setUpdateFieldValByName("modifyName", AuthUtil.getRealName(), metaObject);
        } catch (Exception var3) {
            log.warn("自动填充获取不到用户信息");
        }

        this.setUpdateFieldValByName("modifyTime", System.currentTimeMillis(), metaObject);
    }

}
